package com.myProject.control.expert;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.myProject.bean.TClass;
import com.myProject.bean.TMeet;
import com.myProject.bean.TThing;
import com.myProject.serves.Impl.MeetSevImpl;
import com.myProject.serves.MeetSev;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@WebServlet(name = "MeetServlet", urlPatterns = "/meeting")
public class MeetServlet extends HttpServlet {
    MeetSev ms = new MeetSevImpl();

    /**
     * @param request
     * @param response
     * @return void
     * @author liuyulai
     * @date 21.4.29 10:22
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    /**
     * @param request
     * @param response
     * @return void
     * @author liuyulai
     * @date 21.4.29 10:22
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String med = request.getParameter("method");
        if (med == null || "".equalsIgnoreCase(med)) {
            showAllMeet(request, response);
        } else if ("add".equalsIgnoreCase(med)) {
            System.out.println("进入了添加");
            try {
                addMeet(request, response);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if ("showOneMeet".equalsIgnoreCase(med)) {
            showOneMeet(request, response);
        }
    }

    private void addMeet(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        String result = request.getParameter("result");
        String people = request.getParameter("expertName");
        String date = request.getParameter("date");
        String thingId = request.getParameter("thingId");
        TMeet tMeet = new TMeet();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (date != null) {
            tMeet.setDate(sdf.parse(date));
        }
        int fkThingId = Integer.parseInt(thingId);
        TThing tThing = new TThing();
        tThing.setPkId(fkThingId);
        tMeet.settThing(tThing);
        tMeet.setResult(result);
        tMeet.setPeople(people);
        boolean flag = ms.addMeet(tMeet);
        System.out.println("flag = " + flag);
    }

    private void showOneMeet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        int pageNo = 1;
        String Page = request.getParameter("page");
        if (Page != null) {
            pageNo = Integer.parseInt(Page);
        }
        System.out.println("pageNo = " + pageNo);
        String limit = request.getParameter("limit");
        int pageSize = Integer.parseInt(limit);
        TMeet tMeet = new TMeet();
        String id = request.getParameter("id");
        int x = Integer.parseInt(id);
        TThing tt = new TThing();
        tt.setPkId(x);
        tMeet.settThing(tt);
        PageInfo<TMeet> show = ms.getOneMeetPage(pageNo, pageSize, tMeet);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(show);
        System.out.println("json = " + json);
        pw.write(json);
    }

    private void showAllMeet(HttpServletRequest request, HttpServletResponse response) {

    }
}
